﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Thor.Common;
using Thor.Domain;
using Thor.WebRole.Services;

namespace Thor.Models
{
    public class CurrentAvailabilityTimeZone
    {
        #region constructors

        public CurrentAvailabilityTimeZone() { }

        public CurrentAvailabilityTimeZone(Thor.Common.TimeZone timeZone)
            : this(timeZone, string.Empty) { }

        public CurrentAvailabilityTimeZone(Thor.Common.TimeZone timeZone, string selectedCalendarEmail)
        {
            this.TimeZone = timeZone.Name;
            this.Timestamp = timeZone.Now.ToLongDateString();
            this.SelectedCalendarEmail = selectedCalendarEmail;
            SetTimeslots();
            this.Entries = BuildEntriesForTimeZone(timeZone);
        }

        public CurrentAvailabilityTimeZone(string timeZone, string timestamp, DateTime currentTimeslotBegin,
            DateTime currentTimeSlotEnd, DateTime nextTimeslotBegin, DateTime nextTimeslotEnd,
            string selectedCalendarEmail, string message)
        {
            TimeZone = timeZone;
            Timestamp = timestamp;
            CurrentTimeslotBegin = currentTimeslotBegin;
            CurrentTimeslotEnd = currentTimeSlotEnd;
            NextTimeslotBegin = nextTimeslotBegin;
            NextTimeslotEnd = nextTimeslotEnd;
            SelectedCalendarEmail = selectedCalendarEmail;
            Message = message;
            SetTimeslots();
        }

        #endregion

        #region properties

        public string TimeZone { get; set; }
        public string Timestamp { get; set; }
        public DateTime CurrentTimeslotBegin { get; set; }
        public DateTime CurrentTimeslotEnd { get; set; }
        public DateTime NextTimeslotBegin { get; set; }
        public DateTime NextTimeslotEnd { get; set; }
        public string SelectedCalendarEmail { get; set; }
        public string Message { get; set; }
        public List<CurrentAvailabilityCalendarEntry> Entries { get; set; }

        #endregion

        #region methods

        private void SetTimeslots()
        {
            DateTime dateTimeUtc = DateTime.UtcNow;
            TimeZoneManager timeZoneManager = new TimeZoneManager();
            DateTime dateTimeLocal = timeZoneManager.ConvertTimeFromUtc(dateTimeUtc, TimeZone);

            if (dateTimeLocal.Minute < 30)
            {
                CurrentTimeslotBegin = new DateTime(dateTimeLocal.Year, dateTimeLocal.Month, dateTimeLocal.Day, dateTimeLocal.Hour, 0, 0);
                CurrentTimeslotEnd = new DateTime(dateTimeLocal.Year, dateTimeLocal.Month, dateTimeLocal.Day, dateTimeLocal.Hour, 29, 59);
                NextTimeslotBegin = new DateTime(dateTimeLocal.Year, dateTimeLocal.Month, dateTimeLocal.Day, dateTimeLocal.Hour, 30, 0);
                NextTimeslotEnd = new DateTime(dateTimeLocal.Year, dateTimeLocal.Month, dateTimeLocal.Day, dateTimeLocal.Hour, 59, 59);
            }
            else
            {
                CurrentTimeslotBegin = new DateTime(dateTimeLocal.Year, dateTimeLocal.Month, dateTimeLocal.Day, dateTimeLocal.Hour, 30, 0);
                CurrentTimeslotEnd = new DateTime(dateTimeLocal.Year, dateTimeLocal.Month, dateTimeLocal.Day, dateTimeLocal.Hour, 59, 59);

                //Beware midnight ;-)
                if (dateTimeLocal.Hour == 23)
                {
                    var day = dateTimeLocal.Day + 1;

                    NextTimeslotBegin = new DateTime(dateTimeLocal.Year, dateTimeLocal.Month, day, 0, 0, 0);
                    NextTimeslotEnd = new DateTime(dateTimeLocal.Year, dateTimeLocal.Month, day, 0, 29, 59);
                }
                else
                {
                    var hour = dateTimeLocal.Hour + 1;

                    NextTimeslotBegin = new DateTime(dateTimeLocal.Year, dateTimeLocal.Month, dateTimeLocal.Day, hour, 0, 0);
                    NextTimeslotEnd = new DateTime(dateTimeLocal.Year, dateTimeLocal.Month, dateTimeLocal.Day, hour, 29, 59);
                }
            }
        }

        private List<CurrentAvailabilityCalendarEntry> BuildEntriesForTimeZone(Thor.Common.TimeZone timeZone)
        {
            List<CurrentAvailabilityCalendarEntry> result = new List<CurrentAvailabilityCalendarEntry>();

            var svc = new CalendarManagerDomainService();

            var calendars = svc.GetCalendarsByTimeZone(timeZone.Name);

            if (calendars != null)
            {
                foreach (var calendar in calendars)
                {
                    var isCurrentTimeslotBusy = calendar.IsBusy(CurrentTimeslotBegin, CurrentTimeslotEnd);
                    var isCurrentTimeslotOffHours = calendar.IsOffHours(CurrentTimeslotBegin);
                    var currentTimeSlotDisplayText = GetDisplayTextForTimeslot(CurrentTimeslotBegin, CurrentTimeslotEnd, calendar);

                    var isNextTimeslotBusy = calendar.IsBusy(NextTimeslotBegin, NextTimeslotEnd);
                    var isNextTimeslotOffHours = calendar.IsOffHours(NextTimeslotBegin);
                    var nextTimeSlotDisplayText = GetDisplayTextForTimeslot(NextTimeslotBegin, NextTimeslotEnd, calendar);

                    var isSelected = false;

                    if (!string.IsNullOrEmpty(this.SelectedCalendarEmail) && calendar.CalendarEmail == this.SelectedCalendarEmail)
                        isSelected = true;

                    var currentTimeslot = new CalendarEntry();
                    currentTimeslot.Available = !isCurrentTimeslotBusy;
                    currentTimeslot.CalendarEmail = calendar.CalendarEmail;
                    currentTimeslot.CalendarId = calendar.Id;
                    currentTimeslot.CalendarName = calendar.Alias;
                    currentTimeslot.DisplayText = currentTimeSlotDisplayText;
                    currentTimeslot.IsCurrent = true;
                    currentTimeslot.OffHours = isCurrentTimeslotOffHours;
                    currentTimeslot.Time = CurrentTimeslotBegin;

                    var nextTimeslot = new CalendarEntry();
                    nextTimeslot.Available = !isNextTimeslotBusy;
                    nextTimeslot.CalendarEmail = calendar.CalendarEmail;
                    nextTimeslot.CalendarId = calendar.Id;
                    nextTimeslot.CalendarName = calendar.Alias;
                    nextTimeslot.DisplayText = nextTimeSlotDisplayText;
                    nextTimeslot.IsCurrent = true;
                    nextTimeslot.OffHours = isNextTimeslotOffHours;
                    nextTimeslot.Time = NextTimeslotBegin;

                    var item = new CurrentAvailabilityCalendarEntry(isSelected, calendar.CalendarEmail, currentTimeslot, nextTimeslot);

                    if (isSelected)
                        result.Insert(0, item);
                    else
                        result.Add(item);
                }
            }


            return result;
        }

        private string GetDisplayTextForTimeslot(DateTime begin, DateTime end, Calendar calendar)
        {
            var displayText = "free";

            var appointment = calendar.GetAppointment(begin, end);

            if (appointment != null)
            {
                if (calendar.DisplaySubject)
                    displayText = appointment.Subject;
                else
                    displayText = "booked";
            }

            return displayText;
        }

        #endregion
    }

    public class CurrentAvailabilityTimeZoneList : List<CurrentAvailabilityTimeZone>
    {
        #region constructors

        public CurrentAvailabilityTimeZoneList(List<Thor.Common.TimeZone> timeZones)
        {
            foreach (var timeZone in timeZones)
                this.Add(new CurrentAvailabilityTimeZone(timeZone));
        }

        #endregion
    }
}